package com.tutu.service.impl;

import com.tutu.core.api.GatewayApiUrls;
import com.tutu.service.GatewayService;
import com.tutu.utils.HttpRequestUtil;
import com.tutu.utils.JsonUtil;
import com.tutu.utils.UnicodeUtil;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * GatewayServiceImpl
 * Created by 涂奕恒 on 2024/5/25 21:25.
 */
@Service
public class GatewayServiceImpl implements GatewayService {

    @Override
    public List<Map<String, Object>> findList() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = HttpRequestUtil.getCompleteData(GatewayApiUrls.base_prefix + GatewayApiUrls.list, "");

        if (map.get("code").equals(0)) for (Object data : JSONArray.fromObject(map.get("data")))
            list.add(JsonUtil.json2Map(data));

        return list;
    }

    @Override
    public Map<String, Object> modifyInfo(HttpServletRequest request, String path, String comment) {
        LogInfoServiceImpl.saveLogInfo(request, "编辑白名单信息，请求路径：" + path + "，描述信息：" + comment);

        return HttpRequestUtil.getCompleteData(GatewayApiUrls.base_prefix + GatewayApiUrls.modifyInfo, "path=" + UnicodeUtil.unicodeEncode(path) + "&comment=" + UnicodeUtil.unicodeEncode(comment));
    }

    @Override
    public Map<String, Object> delInfo(HttpServletRequest request, String path) {
        LogInfoServiceImpl.saveLogInfo(request, "删除白名单信息，请求路径：" + path);

        return HttpRequestUtil.getCompleteData(GatewayApiUrls.base_prefix + GatewayApiUrls.modifyInfo, "path=" + UnicodeUtil.unicodeEncode(path) + "&flag=1");
    }
}